package com.shg.jpa.helloworld;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.Date;

public class Main {

    public static void main(String[] args) {
        // 1. 创建EntityManagerFactory
        String persistenceUnitName = "default";
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);

        // 2. 创建EntityManager
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        // 3. 开启事务
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        // 进行持久化操作
        Customer customer = new Customer("Tom","234@qq.com",14,new Date(),new Date());
        entityManager.persist(customer);

        // 5. 提交事务
        transaction.commit();

        // 6. 关闭资源
        entityManager.close();
        entityManagerFactory.close();
    }
}
